﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using BlogApi.Models;
using BlogApi.ModelsDto;
using BlogApi.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BlogApi.Controllers
{
    [Route("api/tag")]
    [ApiController]
    public class TagController : ControllerBase
    {
        private readonly IArticleTagRepository _articleTagRepository;
        private readonly IArticleToArticleTagRepository _articleToArticleTagRepository;
        private readonly IMapper _mapper;

        public TagController(IArticleToArticleTagRepository articleToArticleTagRepository,IArticleTagRepository articleTagRepository,IMapper mapper)
        {
            _articleTagRepository = articleTagRepository ?? throw new ArgumentNullException(nameof(articleTagRepository));
            _articleToArticleTagRepository = articleToArticleTagRepository ?? throw new ArgumentNullException(nameof(articleToArticleTagRepository));
            _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
        }
        [HttpPost]
        [Route("addTag")]
        public async Task<IActionResult> AddArticleTag([FromBody]TagAddDto tagAddDto)
        {
            if(await _articleTagRepository.UserArticleTagNameExistsAsync(tagAddDto.TagName, tagAddDto.UserId))
            {
                return new JsonResult(new
                {
                    status = "error",
                    message = "该用户的TAG已经存在"
                });
            }
            var articleTagEntity = _mapper.Map<ArticleTag>(tagAddDto);
            _articleTagRepository.AddArticleTag(articleTagEntity);
            if(await _articleTagRepository.SaveAsync())
            {
                var newTagDto = _mapper.Map<TagDto>(articleTagEntity);
                return new JsonResult(new
                {
                    status = "ok",
                    newTag = newTagDto
                });
            }
            else
            {
                return BadRequest();
            }

        }
        [HttpGet]
        [Route("tagList/{userId}/{index}")]
        public async Task<IActionResult> GetTagList([FromRoute]Guid userId,[FromRoute]int index)
        {
            var tagEntityList = await _articleTagRepository.GetUserArticleTagsByPageAsync(userId, 6, index);
            var tagDtoList = _mapper.Map<IEnumerable<TagDto>>(tagEntityList);
            return new JsonResult(new
            {
                code = 0,
                tagList = tagDtoList
            });
        }
        [HttpPost]
        [Route("deleteTag")]
        public async Task<IActionResult> DeleteTag([FromBody]TagDeleteDto tagDeleteDto)
        {
            var tagEntity = await _articleTagRepository.GetArticleTagAsync(tagDeleteDto.TagId);
            _articleTagRepository.DeleteArticleTag(tagEntity);
            if(await _articleTagRepository.SaveAsync())
            {
                return new JsonResult(new
                {
                    status = "ok"
                });
            }
            else
            {
                return BadRequest();
            }
        }
        [HttpPost]
        [Route("modifyTag")]
        public async Task<IActionResult> ModifyTag([FromBody]TagModifyDto tagModifyDto)
        {
            if(await _articleTagRepository.UserArticleTagNameExistsAsync(tagModifyDto.TagName, tagModifyDto.UserId))
            {
                return new JsonResult(new
                {
                    status = "error",
                    message = "该用户的TAG已经存在"
                });
            }
            var tagEntity = await _articleTagRepository.GetArticleTagAsync(tagModifyDto.TagId);
            tagEntity.TagName = tagModifyDto.TagName;
            tagEntity.CreateTime = DateTime.Now;
            _articleTagRepository.UpdateArticleTag(tagEntity);
            if(await _articleTagRepository.SaveAsync())
            {
                return new JsonResult(new
                {
                    status = "ok"
                });
            }
            else
            {
                return BadRequest();
            }
        }
        [HttpGet]
        [Route("articleTag/{articleId}")]
        public async Task<IActionResult> GetArticleTag([FromRoute]Guid articleId)
        {
            var articleToArticleTagList = await _articleToArticleTagRepository.GetArticleToArticleTagsByArticleIdAsync(articleId);
            List<TagDto> tagList= new List<TagDto>();
            foreach(var item in articleToArticleTagList)
            {
                tagList.Add(_mapper.Map<TagDto>(await _articleTagRepository.GetArticleTagAsync(item.ArticleTagId)));
            }
            return new JsonResult(new
            {
                articleTagList = tagList
            });
        }
    }
}